package com.usian.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.article.AuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/author")
public class ApAuthorController implements AuthorControllerApi {

    @Autowired
    private ApAuthorService apAuthorServiceI;

    @Override
    @GetMapping("/findByUserId/{id}")
    public ApAuthor findByUserId(@PathVariable Integer id) {
        LambdaQueryWrapper<ApAuthor> authorLambdaQueryWrapper =
                Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, id);
        List<ApAuthor> apAuthorList = apAuthorServiceI.list(authorLambdaQueryWrapper);
        if (apAuthorList != null && !apAuthorList.isEmpty()) {
            return apAuthorList.get(0);
        }
        return null;
    }

    @Override
    @PostMapping("/save")
    public boolean save(@RequestBody ApAuthor apAuthor) {

        return apAuthorServiceI.save(apAuthor);
    }

    @Override
    @GetMapping("findByName/{name}")
    public ApAuthor findByName(@PathVariable String name) {
        LambdaQueryWrapper<ApAuthor> queryWrapper = Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name);
        return apAuthorServiceI.getOne(queryWrapper);
    }

    @Override
    @GetMapping("/findById/{id}")
    public ApAuthor findById(@PathVariable Integer id) {
        return apAuthorServiceI.getById(id);
    }
}
